package com.project.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service;

    @RequestMapping("findAll")
    public PageInfo<UserBean>findAll(int  pageNo){
        PageInfo<UserBean>pageInfo=service.findAll(pageNo);
        return pageInfo;
    }

    @RequestMapping("add")
    public String add(@Valid UserBean user, BindingResult result,HttpServletRequest request) throws Exception {
        if (result.hasErrors()){
            List<FieldError>errorList=result.getFieldErrors();
            String str =new ObjectMapper().writeValueAsString(errorList);
            return str;
        }
        service.add(user);
        return "ok";
    }
}
